#include "rk_debug.h"
#include <string.h>
#include <sys/time.h>

#define YUV_H 240
#define YUV_W 320

#define MODEL_X1 50 //起点位置
#define MODEL_Y1 103 //起点位置
#define MODEL_X2 242 //终点位置
#define MODEL_Y2 190 //终点位置


static inline int64_t getCurrentTimeUs()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec * 1000000 + tv.tv_usec;
}


int main(int argc, const char **argv)
{

    int frameWidth =320;
    int frameHeight = 240;

    int xRange = 242 - 50;
    int yRange = 190 - 103;

    FILE *file_;
    char buffer[frameWidth*frameHeight]; // 320*240
    file_ = fopen("/data/test1.YUV", "rb+");
    while (!feof(file_))
    {
        fread(buffer, sizeof(buffer), 1, file_);
    }
    RK_U8 * bufferTmpData = (RK_U8 *)buffer; //读取图像数据

    RK_U8 pTmpData[1][yRange][xRange][1]; //分割后的图像
    
        int64_t start_us = getCurrentTimeUs();
        for (int i = 0; i < YUV_W; i++)
        {
            for (int j = 0; j < YUV_H; j++ )
            {
                if(i >= MODEL_X1 && i <= MODEL_X2 && j >= MODEL_Y1 && j <= MODEL_Y2)
                {
            
                    pTmpData[0][i- MODEL_X1][j - MODEL_Y1][0] = *bufferTmpData;
                }
                bufferTmpData += 1;
            }
        }
        
        int64_t elapse_us = getCurrentTimeUs() - start_us;
        printf("Elapse Time = %.2fms, FPS = %.2f\n", elapse_us / 1000.f, 1000.f * 1000.f / elapse_us);
    

    
    RK_U8 *tmpDta = (RK_U8 *)pTmpData;

    FILE *file_fenge = NULL;
    file_fenge = fopen("/data/fenge.YUV", "wb");
    fwrite(tmpDta, 1, (yRange*xRange), file_fenge);
    fclose(file_fenge);
 

    return 0;
}
